//
//  RgisterViewController.m
//  Remind
//
//  Created by macZF on 2017/10/30.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "RgisterViewController.h"
#import "DataCach.h"
@interface RgisterViewController () <UITextFieldDelegate>

@end

@implementation RgisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"注册";
    [DataCach addTextFieldInputAccessoryView:_userTextField];
    [DataCach addTextFieldInputAccessoryView:_passwordTextField];
    _passwordTextField.secureTextEntry = YES;
}

- (IBAction)onClickDoneButton:(id)sender {
   // [[NSUserDefaults standardUserDefaults] setObject:[NSMutableDictionary new] forKey:@"users"];
    
    NSMutableDictionary *users = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"users"]];
    NSLog(@"用户列表%@",users);
    if(!users) users = [NSMutableDictionary dictionary];
    
    NSString *massage = @"注册成功";
    for (NSString *key in users.allKeys) {
        if([key isEqualToString:_userTextField.text]){
            massage = @"用户名已存在";
        }
    }
    if([_passwordTextField.text isEqualToString:@""]||[_userTextField.text isEqualToString:@""]){
        massage = @"请输入账号/密码";
    }
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:massage preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if([massage isEqualToString:@"注册成功"]){
             [users setObject:_passwordTextField.text forKey:_userTextField.text];
             NSLog(@"%@",users);
            
           
            [[NSUserDefaults standardUserDefaults] setObject:users forKey:@"users"];
            [self.navigationController popViewControllerAnimated:YES];
            
            NSLog(@"----注册成功----");
        }
    }];
    [alertCon addAction:action];
    [self.navigationController presentViewController:alertCon animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
